/*
 * @lc app=leetcode.cn id=883 lang=javascript
 *
 * [883] 三维形体投影面积
 */

// @lc code=start
/**
 * @param {number[][]} grid
 * @return {number}
 */
var projectionArea = function (grid) {
  let row = [];
  let col = [];
  let bottom = 0;
  for (let i = 0; i < grid.length; i++) {
    for (let j = 0; j < grid[i].length; j++) {
      row[i] = row[i] ? Math.max(grid[i][j], row[i]) : grid[i][j];
      col[j] = col[j] ? Math.max(grid[i][j], col[j]) : grid[i][j];
      if (grid[i][j] > 0) {
        bottom++;
      }
    }
  }
  return bottom + row.reduce((c, p) => c + p) + col.reduce((c, p) => c + p);
};
// @lc code=end

